package servlets;

import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import database.GestioneCateringDB;

/**
 * Servlet implementation class EventServlet2
 */
@WebServlet("/EventServlet")
public class EventServlet extends HttpServlet {
	
public void service(HttpServletRequest req , HttpServletResponse res)
{
	
	// reperire con codice db lista ID evento ammissibili
	
	res.setContentType("text/html");
	
    PrintStream ps = null;
	
	try
	{
		ps = new PrintStream(res.getOutputStream());
	}
	catch (IOException e)
	{
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
	GestioneCateringDB cat = null;
	
	try
	{
		cat = new GestioneCateringDB();
	} 
	catch (ClassNotFoundException e1)
	{
		// TODO Auto-generated catch block
		e1.printStackTrace();
	}
	
	
	ArrayList<String> ids = cat.getEventIds();
	
	String idsHtml = "";
	for(String a : ids){
		idsHtml = idsHtml + " <option value=\""+a+"\">"+a+"</option>";
	}
	
	ps.print("<html><head></head><body>");
	ps.print("<form align = \"center\" action = \" http://127.0.0.1:8080/Caterone/AdditionalInformationServlet\"  method= \"post\">");
	ps.print(" Seleziona evento : <select name=\"id_evento\"> " 
				+ idsHtml
				+ " </select> ");
	ps.print("<input type = \"submit\" value = \"Seleziona\"></form><br>");
	
	// se invocata da altra servlet
	if( req.getAttribute("id_evento") != null )
	{
		ps.print("<hr width= \"80%\" align=\"center\" size = \"8" +"\" >");
		
		ps.println("<div align = \"center\" >");
		
		ps.println("<pre>");
		
		ps.println( "ID evento :" +  (String) req.getAttribute("id_evento"));
		
		ps.println("Tipo allestimento :" +  (String) req.getAttribute("tipo_allestimento"));
		
		ps.println("Tipo servizio :" +  (String) req.getAttribute("tipo_servizio"));
		
		ps.println( "Numero invitati :" +  req.getAttribute("n_invitati"));
		
		ps.println( "Presenza Artista :" +  req.getAttribute("is_artista"));
		
		// pulsante avanti
		ps.print("<form align = \"center\" action = \" http://127.0.0.1:8080/Caterone/PrelevaOrdinaServlet\"  method= \"post\">");
		ps.print("<input type = \"hidden\" name =  \"id_evento\" value = \""+ (String) req.getAttribute("id_evento") + "\" ><br>");
		ps.print("<input type = \"submit\" value = \"Avanti\"></form><br>");
		
		ps.println("</pre>");
		
		ps.println("</div>");
	}	
	
	ps.print("</body></html>");
	
	ps.close();
	
}

}
